const app = getApp()
var bmap=require("./bmap-wx.min.js")
Page({
  data: {
    auther:false,//是否授权
    user:{},
    region:"北京市海淀区",
    ak:"MzN2nI2SVmY6xo2rRCuiWd54HGbVgFq7",
    urlList:[{item:"富文本编辑",url:'../editor/editor'},
        {item:"手动签名",url:"../sign/sign"},
      {item:'组件通信',url:'../parentMsg/index'},
      {item:"canvas保存图片",url:'../canvasImg/canvasImg'},
    {item:"echart",url:'../echart/echart'}]
  },
  //上传图片base64格式
  imgBtn(){
    wx.chooseImage({
      count:1,
      sizeType:["compressed"],
      sourceType:['ablum','camera'],
      success:res=>{
        let path=res.tempFilePaths[0];
        wx.getFileSystemManager().readFile({
          filePath:path,
          encoding:'base64',
          success:res=>{
            let imgBase64='data:image/png;base64,'+res.data;
            console.log("img base64",imgBase64)
          }
        })
      },
    })
  },
  goPage(e){
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },
  getLocation(){
    let e=this;
    wx.getLocation({
      //type: 'wgs84',
      type:"gcj02",
      altitude: true,
      success: res=>{
        console.log("location",res)
        const latitude = res.latitude
        const longitude = res.longitude;
      // wx.chooseLocation({
      //   latitude:latitude, 
      //   longitude:longitude,
      //   success:data=>{
      //     console.log("address",data)
      //   },
      //   fail:data=>{
      //     console.log("fail",data)
      //   },
      // });
       
        var BMap = new bmap.BMapWX({
          ak: e.data.ak
        });
        var fail = function (data) {
          console.log(data);
        };
        var success = function (data) {
          //返回数据内，已经包含经纬度  
          console.log(data);
          //使用wxMarkerData获取数据  
          let wxMarkerData = data.wxMarkerData;
          //把所有数据放在初始化data内  
          e.setData({
            markers: wxMarkerData,
            latitude: wxMarkerData[0].latitude,
            longitude: wxMarkerData[0].longitude,
            address: wxMarkerData[0].address,
            cityInfo: data.originalData.result.addressComponent
          });
        }
        // 发起regeocoding检索请求   
        BMap.regeocoding({
          fail: fail,
          success: success
        });
       }
    })
  },
  onLoad: function () {
    var e=this;
    this.getLocation()
    wx.getUserInfo({
      success:res=>{
        //console.log(res.userInfo);
        e.setData({auther:true,user:res.userInfo})
      },
      fail:res=>{
        e.setData({auther:false})
      }
    })
  },
  getInfo(){
    let e=this;
    wx.login({
      success(r){
        console.log(r.userInfo);
        e.setData({ auther: true, user: r.userInfo})
      }
    })
  }
})
